﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using System.Threading;
namespace FOSystem.CultureHelper
{
    public class CultureController : Controller
    {
        protected override void ExecuteCore()
        {
            string cultureName = "vi-VN";
            // Attempt to read the culture cookie from Request
            HttpCookie cultureCookie;
            if (Request.Cookies["_culture"] == null)
            {
                cultureCookie = new HttpCookie("_culture");
                cultureCookie["lang"] = CultureHelper.GetValidCulture(cultureName);
                Response.Cookies.Add(cultureCookie);
            }
            else
            {
                cultureCookie = Request.Cookies["_culture"];
                if (cultureCookie != null)
                {
                    cultureName = cultureCookie["lang"];
                    cultureName = CultureHelper.GetValidCulture(cultureName); // This is safe
                }
                else if (Request.UserLanguages != null)
                {
                    // The user agent sent a Accept-Language request header so attempt to read its value
                    cultureName = Request.UserLanguages[0];
                    cultureName = CultureHelper.GetValidCulture(cultureName); // This is safe
                    cultureCookie["lang"] = cultureName;
                    Response.Cookies.Add(cultureCookie);//
                }
                else
                {
                    cultureName = CultureHelper.GetDefaultCulture();
                    cultureCookie["lang"] = cultureName;
                    Response.Cookies.Add(cultureCookie);//
                }
            }
          
            // Modify current thread's cultures    
            CultureInfo cul = CultureInfo.CreateSpecificCulture(cultureName);
            if (cultureName == "vi-VN")
            {
               cul.NumberFormat.NumberDecimalSeparator = ".";
            }

            Thread.CurrentThread.CurrentCulture = cul;
            Thread.CurrentThread.CurrentUICulture = cul;
            base.ExecuteCore();

        }

    }
}
